package com.avic.gateway.common.util;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @Description base64加密算法
 * @Author huangsheng
 * @Date 2018/10/14 9:32
 */
@Slf4j
public class Base64Util {

    public static final Base64.Decoder DECODER = Base64.getDecoder();

    public static final Base64.Encoder ENCODER = Base64.getEncoder();

    public static final String UTF8 = "UTF-8";


    /**
     * 编码
     *
     * @param resource
     * @return
     */
    public static String encodeToString(String resource) {
        try {
            final byte[] textByte = resource.getBytes(UTF8);
            return ENCODER.encodeToString(textByte);
        } catch (UnsupportedEncodingException e) {
            log.error("base64加密员工信息时发生错误{}", e.getMessage());
            throw new RuntimeException();
        }

    }

    /**
     * 解码
     *
     * @param resource
     * @return
     */
    public static String decode(String resource) {
        try {
            return new String(DECODER.decode(resource), UTF8);
        } catch (UnsupportedEncodingException e) {
            log.error("base64解密员工信息时发生错误{}", e.getMessage());
            throw new RuntimeException();
        }
    }
}
